<template>
  <div id="app">
    <div class="container">
      <div class="close"><span class="iconfont iconicon-test"></span></div>
      <div class="logo"><span class="iconfont iconnew"></span></div>
      <div class="inputs">
        <hmInput
          type="text"
          v-model="user.username"
          placeholder="请输入手机号"
          :reg="/^1[3,4,5,6,7,8,9]\d{9}$/"
          ref="username"
        ></hmInput>
        <hmInput
          placeholder="请输入昵称"
          v-model="user.nickname"
          ref="nickname"
        ></hmInput>
        <hmInput
          type="password"
          v-model="user.password"
          placeholder="请输入密码"
          :reg="/^\d{3,}$/"
          ref="password"
        ></hmInput>
      </div>
      <p class="tips">
        有账号？
        <a href="#/login" class="">去登录</a>
      </p>
      <van-button color="green" round type="primary" block @click="registerFn"
        >注册</van-button
      >
    </div>
  </div>
</template>

<script>
// 导入封装好的登录请求
import { register } from "../api/user";
export default {
  data() {
    return {
      user: {
        username: "",
        password: "",
        nickname: "",
      },
    };
  },
  methods: {
    registerFn() {
      //请i去数据前，潘顿用户输入的内容是否合规则（正确）
      //检擦手机号码
      if (!this.$refs.username.isOk) {
        this.$toast.fail({
          message: "手机号码不正确，请检查",
          icon: "like-o",
        });
        return;
      }
      //检擦昵称
      if (!this.$refs.nickname.isOk) {
        this.$toast.fail({
          message: "昵称不能为空",
          icon: "like-o",
        });
        return;
      }
      //   检擦密码
      if (!this.$refs.password.isOk) {
        this.$toast.fail({
          message: "密码长度3-10位，清检查",
          icon: "like-o",
        });
        return;
      }

      console.log(123);
      register(this.user).then((res) => {
        console.log(res);
        if (res.data.message === "注册成功") {
          //注册成功提示
          this.$toast.success({
            message: res.data.message,
            icon: "like-o",
          });
          //注册成功跳转页面
          this.$router.push({
            path: "/login",
          });
        } else {
          // 轻提示
          this.$toast.fail({
            message: res.data.message,
            icon: "like-o",
          });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 20px;
}
.close {
  span {
    font-size: (27 / 360) * 100vw;
  }
}
.logo {
  display: flex;
  justify-content: center;

  span {
    display: block;
    font-size: (126 / 360) * 100vw;
    color: #d81e06;
  }
}
.inputs {
  input {
    margin-bottom: 20px;
  }
}
.tips {
  text-align: right;
  margin-bottom: 20px;
  a {
    color: #3385ff;
  }
}
</style>